import {
  conferRecordContentType,
  conferRecordType,
  conferType,
  partyGroupType
} from '@/api/zzxt/party-conference/dictionary'
import { partyDuties, typeInParty } from '@/api/zzxt/party-userinfo'
import { getRankType } from '@/api/apply/query'
import { getExtendTypes } from '@/api/zzxt/party-conference/confer-extend-fields'

const state = {
  conferRecordContentTypes: null,
  conferRecordContentTypesDict: null,
  conferRecordContentTypesTarget: {
    confer: {},
    records: {}
  },
  conferRecordTypes: null,
  conferRecordTypesDict: null,
  conferRecordHistory: null,
  conferRecordHistoryDict: null,
  conferContentHost: null,
  conferTypes: null,
  conferTypesDict: null,
  partyGroupType: null,
  partyGroupTypeDict: null,
  partyGroupItemDict: {},
  partyDuties: null,
  partyDutiesDict: null,
  RankType: null,
  RankTypeDict: null,
  typeInParty: null,
  typeInPartyDict: null,
  conferenceExtendFieldDict: null,
  initConferenceExtendFieldsDictionaryLoading: false
}
const mutations = {
}
const initConferRecordContent = (state) => {
  const list = state.conferRecordContentTypes
  const target = state.conferRecordContentTypesTarget
  if (!list) return
  list.forEach(v => {
    v.conferTypeDict = {}
    v.conferType.forEach(t => {
      if (!target.confer[t]) target.confer[t] = []
      target.confer[t].push(v.value)
    })
    v.recordTypeDict = {}
    v.recordType.forEach(t => {
      if (!target.records[t]) target.records[t] = []
      target.records[t].push(v.value)
    })
  })
  target.confer.get = (type) => target.confer[type] || []
}
const initDictionaryConfig = (state) => {
  state.conferContentHost = host_dict(state)
  initConferRecordContent(state)
}
const host_dict = (state) => {
  const dict = state.conferRecordTypesDict
  if (!dict) return null
  const result = Object.values(dict).find(i => i.alias === '主持')
  if (!result) console.error('未找到主持人字典，请联系管理员！')
  return result
}
const actions = {
  initDictionary({
    commit,
    state
  }) {
    return new Promise((res, rej) => {
      if (state.conferTypes) return res(state)
      state.conferTypes = []
      Promise.all([conferRecordContentType(), conferRecordType(), conferType(), partyGroupType(), partyDuties(), getRankType(), typeInParty()]).then(datas => {
        const toHandleList = ['conferRecordContentTypes', 'conferRecordTypes', 'conferTypes', 'partyGroupType', 'partyDuties', 'RankType', 'typeInParty']
        toHandleList.map((name, index) => {
          state[name] = datas[index] && datas[index].list
          const key = `${name}Dict`
          const dict = {}
          state[key] = dict

          if (!state[name]) return
          const first = state[name][0]
          const value_key = (first.value === undefined) ? 'id' : 'value'
          state[name].map(i => {
            dict[i[value_key]] = i
          })
        })
        initDictionaryConfig(state)
        res(state)
      }).catch(e => {
        state.conferTypes = null // 重置加载状态
        rej(e)
      })
    })
  },
  initConferenceExtendFieldsDictionary({ commit, state }, { reload }) {
    return new Promise((res, rej) => {
      if (state.initConferenceExtendFieldsDictionaryLoading && !reload) return res(state)
      state.initConferenceExtendFieldsDictionaryLoading = true
      getExtendTypes({}).then(data => {
        state.conferenceExtendFieldDict = {}
        data.list.map(i => {
          state.conferenceExtendFieldDict[i.id] = i
        })
      }).catch(e => {
        state.conferenceExtendFieldDict = null
        rej(e)
      }).finally(() => {
        state.initConferenceExtendFieldsDictionaryLoading = false
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
